package com.cz.nongyetongv5.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.AssistSuperviseBuy;
import com.cz.nongyetongv5.bean.AssistSuperviseBuyBean;
import com.cz.nongyetongv5.bean.AssistSuperviseBuyDetail;
import com.cz.nongyetongv5.bean.AssistSuperviseBuyListResponse;
import com.cz.nongyetongv5.bean.JsonUtil;
import com.cz.nongyetongv5.bean.NzdBuyPic;
import com.cz.nongyetongv5.bean.PatrolPic;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;


public class AssistSuperviseBuyDetailActivity extends FragmentActivity implements View.OnClickListener {

    private RelativeLayout rl_back;
    private TextView tv_title;


    private TextView tv_name;
    private TextView tv_date;
    private TextView tv_type;
    private TextView tv_nzd_name;
    private TextView tv_dengji;
    private TextView tv_changjia;
    private TextView tv_danwei;
    private TextView tv_count;
    private RecyclerView lv_patrol_pic;
    private RelativeLayout rl_patrol_pic;
    private AssistSuperviseBuyDetail nzdBuy;
    private List<NzdBuyPic> nzdBuyPics;
    private PhotoAdapter adapter;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assist_supervise_buy);
        rl_back = findViewById(R.id.rl_back);
        rl_back.setOnClickListener(this);
        tv_title = findViewById(R.id.tv_title);
        tv_title.setText("投入品采购详情");
        nzdBuy = (AssistSuperviseBuyDetail) getIntent().getSerializableExtra("nzdBuy");

        tv_name = findViewById(R.id.tv_name);
        tv_date = findViewById(R.id.tv_date);
        tv_type = findViewById(R.id.tv_type);
        tv_nzd_name = findViewById(R.id.tv_nzd_name);
        tv_dengji = findViewById(R.id.tv_dengji);
        tv_changjia = findViewById(R.id.tv_changjia);
        tv_danwei = findViewById(R.id.tv_danwei);
        tv_count = findViewById(R.id.tv_count);

        lv_patrol_pic = findViewById(R.id.lv_patrol_pic);
        rl_patrol_pic = findViewById(R.id.rl_patrol_pic);


        if (nzdBuy != null) {
            if (nzdBuy.getPhoto() == null || nzdBuy.getPhoto().isEmpty()) {
                rl_patrol_pic.setVisibility(View.GONE);
            } else {
                rl_patrol_pic.setVisibility(View.VISIBLE);
                nzdBuyPics = nzdBuy.getPhoto();
                adapter = new PhotoAdapter();
                lv_patrol_pic.setLayoutManager(new GridLayoutManager(AssistSuperviseBuyDetailActivity.this, 3));
                lv_patrol_pic.setAdapter(adapter);
                adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                        List<PatrolPic> picBeenList = new ArrayList<>();
                        for (NzdBuyPic pic :
                                nzdBuy.getPhoto()) {
                            picBeenList.add(new PatrolPic(pic.getAttach_url()));
                        }
                        Intent intent = new Intent(AssistSuperviseBuyDetailActivity.this, NzdPicDetailActivity.class);
                        intent.putExtra("position", position);
                        intent.putExtra("photos", (Serializable) picBeenList);
                        startActivity(intent);
                    }
                });
            }

            tv_name.setText(nzdBuy.getSupervise_name());

            tv_date.setText(nzdBuy.getBuy_date());
            tv_type.setText(nzdBuy.getPesticide_type_name());
            tv_nzd_name.setText(nzdBuy.getPesticide_name());
            tv_dengji.setText(nzdBuy.getRegistration_number());
            tv_changjia.setText(nzdBuy.getManufacturer());
            tv_danwei.setText(nzdBuy.getBuy_enterprises());
            tv_count.setText(nzdBuy.getBuy_number() + nzdBuy.getNumber_unit_name());
        }

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_back:
                finish();
                break;

            default:
                break;
        }
    }

    public class PhotoAdapter extends BaseQuickAdapter<NzdBuyPic, BaseViewHolder> {
        public PhotoAdapter() {
            super(R.layout.product_pic_item, nzdBuyPics);
        }

        @Override
        protected void convert(BaseViewHolder helper, NzdBuyPic item) {
            Glide.with(mContext)
                    .load(item.getAttach_url()).placeholder(R.drawable.loading)
                    .into((ImageView) helper.getView(R.id.iv_iv));

        }
    }

}
